let app = getApp();
Page({

  data: {
    closeFlag : false,
  },

  onLoad: function(options) {
    //正在热映
    let inTheatersUrl = 'in_theaters?start=0&count=10';
    this.getMovieDataList(inTheatersUrl, 'inTheater','正在热映');
    //即将上映
    let comingSoonUrl = 'coming_soon?start=0&count=10';
    this.getMovieDataList(comingSoonUrl, 'comingSoon','即将上映');
    //TOP250
    let top250Url = 'top250?start=0&count=10';
    this.getMovieDataList(top250Url, 'top250','豆瓣TOP250');
  },

  //1.获取电影数据
  getMovieDataList(movieUrl, movieType,categoryTitle) {
    app.getGlobalData(app.globalData.doubanApi + movieUrl).then( (res)=> {
      let allMovieData = {};        // allMovieData : 分别保存 正在热映，即将上映，TOP250
      allMovieData[movieType] = {
        movies: res.data.subjects,
        categoryTitle: categoryTitle,
      }
      this.setData(allMovieData);
    },  (err) =>{

    });
  },

  //2.加载更多
  getMore(e){
    let categoryTitle = e.currentTarget.dataset.category
    wx.navigateTo({
      url: '/pages/movies/more-movie/more-movie?categoryTitle=' + categoryTitle,
    })
  },

  //3.获取搜索焦点事件
  onBindFocus(e){
    this.setData({
      closeFlag : true,
    });
  },

  //4.关闭搜索事件
  onCloseSearch(){
    this.setData({
      closeFlag: false,
    });
  },
  
  //5.去到电影详情页面
  getMovieInfo:function(e){
    wx.navigateTo({
      url: '/pages/movies/movie-info/movie-info?id=' + e.currentTarget.dataset.movieId,
      success:(res)=>{
      },
    })
  },
})